package com.dgut.demo.service.impl;

import com.dgut.demo.bean.EmployeesMessage;
import com.dgut.demo.bean.GoodsMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.bean.VipMessage;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.mapper.EmployeesMapper;
import com.dgut.demo.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/4 23:28
 * @Version 1.0
 */
@Service
public class EmployeesServiceImpl implements EmployeesService {
    @Autowired
    private EmployeesMapper employeesMapper;
    @Autowired
    private LogsController logsController;
    @Autowired
    private User user;

    @Override
    public List<EmployeesMessage> getAllEmployeesMessage() {
        return employeesMapper.getAllEmployeesMessage();
    }

    @Override
    public int addOneEmployeesMessage(EmployeesMessage employeesMessage) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String eid='4'+ dateFormat1.format(date) + random;
        employeesMessage.setEid(eid);

        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        String ehiredate = dateFormat2.format(date);
        employeesMessage.setEhiredate(date.valueOf(ehiredate));
        int flag;
        flag = employeesMapper.addOneEmployeesMessage(employeesMessage);
        if(flag == 1){
            //log
            String actions = "新增供应商【"+employeesMapper.getEmployeesMessageById(eid).toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteOneEmployeesMessage(String eid) {
        EmployeesMessage employeesMessage = employeesMapper.getEmployeesMessageById(eid).get(0);
        //log
        String actions = "删除员工【"+employeesMessage.toString()+"】";
        String userId = user.getUserId();
        logsController.addOneLogsMessage(actions,userId);

        return employeesMapper.deleteOneEmployeesMessage(eid);
    }

    @Override
    public int updateOneEmployeesMessage(EmployeesMessage employeesMessage) {
        EmployeesMessage employeesMessage1 = employeesMapper.getEmployeesMessageById(employeesMessage.getEid()).get(0);
        int flag = employeesMapper.updateOneEmployeesMessage(employeesMessage);
        if(flag == 1){
            EmployeesMessage employeesMessage2 = employeesMapper.getEmployeesMessageById(employeesMessage.getEid()).get(0);
            //log
            String actions = "更新员工信息【"+employeesMessage1.toString()+"】--》【"+employeesMessage2.toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
        }
        return flag;
    }

    @Override
    public EmployeesMessage getOneEmployeesMessage(String eid) {
        return employeesMapper.getOneEmployeesMessage(eid);
    }

    @Override
    public List<EmployeesMessage> getEmployeesMessageById(String eid) {
        return employeesMapper.getEmployeesMessageById(eid);
    }

    @Override
    public List<EmployeesMessage> getEmployeesMessageByName(String ename) {
        return employeesMapper.getEmployeesMessageByName(ename);
    }

    @Override
    public List<EmployeesMessage> getEmployeesMessageByPhone(String ephone) {
        return employeesMapper.getEmployeesMessageByPhone(ephone);
    }

    @Override
    public List<EmployeesMessage> getEmployeesMessageByDepartment(String edepartment) {
        return employeesMapper.getEmployeesMessageByDepartment(edepartment);
    }
}
